//
//  BillViewController.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/1.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KABillViewController.h"
#import "KABillTableViewCell.h"
#import "KAAddBillViewController.h"
#import "KAButton.h"
#import "KADataBaseModel.h"

@interface KABillViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UIButton *dateBtn;
@property(nonatomic, strong) UILabel *placeselabel;
@property(nonatomic, strong) UITableView *billTableView;

@property(nonatomic, strong) NSMutableArray *billArr;

@property(nonatomic, strong) UILabel *incomeLabel;
@property(nonatomic, strong) UILabel *outlayLabel;
//总收支
@property(nonatomic, assign) NSInteger incomeSum;
@property(nonatomic, assign) NSInteger outlaySum;
@end

static NSString *const billTableViewCell = @"KABillTableViewCell";

@implementation KABillViewController

-(void)viewWillAppear:(BOOL)animated
{
    [self.billArr removeAllObjects];
    [self querydbData];
}

-(void) querydbData
{
    NSLog(@"%@",_dateBtn.titleLabel.text);
    [self.billArr removeAllObjects];
    NSArray *arrDict = [[KADataBase instance] queryWithDate:_dateBtn.titleLabel.text];
    for (NSDictionary *dict in arrDict) {
        KADataBaseModel *dbModel = [KADataBaseModel dbWithDict:dict];
        [self.billArr addObject:dbModel];
    }
    [self.billTableView reloadData];
    [self loadIncomeWithOutlayData];
}

-(void) setNavBar
{
    NSLog(@"%@",NSHomeDirectory());
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *nowDate = [dateFormatter stringFromDate:[NSDate date]];
    UIView *view = [UIView new];
    view.frame = CGRectMake(0, 0, KAScreenW, 44);
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
    label.text = @"账单";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:18];
    [view addSubview:label];
    
    UIButton *button = [[UIButton alloc]init];
    button.bounds = CGRectMake(0, 0, 100, 44);
    button.center = CGPointMake((KAScreenW - 30) / 2 , 44 / 2);
    [button setTitle:nowDate forState:UIControlStateNormal];
    [button addTarget:self action:@selector(switchDate:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    _dateBtn = button;
    self.navigationItem.titleView = view;
}

-(void) switchDate:(UIButton *)sender
{
    [BAKit_DatePicker ba_creatPickerViewWithType:BAKit_CustomDatePickerDateTypeYMD configuration:^(BAKit_DatePicker *tempView) {
        tempView.ba_pickViewTitleColor = KANavTabColor;
        // 自定义 pickview title 的字体
        tempView.ba_pickViewTitleFont = [UIFont boldSystemFontOfSize:18];
        // 自定义：pickView 位置
        tempView.pickerViewPositionType = BAKit_PickerViewPositionTypeCenter;
        // 自定义：toolBar 位置
        tempView.buttonPositionType = BAKit_PickerViewButtonPositionTypeBottom;
        // 自定义：pickView 文字颜色
        tempView.ba_pickViewTextColor = KANavTabColor;
        // 自定义：pickView 文字字体
        tempView.ba_pickViewFont = [UIFont systemFontOfSize:13];
        // 可以自由定制按钮颜色
        tempView.ba_buttonTitleColor_sure = [UIColor redColor];
        tempView.ba_buttonTitleColor_cancle = [UIColor blueColor];
    } block:^(NSString *resultString) {
        [self.dateBtn setTitle:resultString forState:UIControlStateNormal];
        [self querydbData];
    }];
}

-(NSMutableArray *)billArr
{
    if (!_billArr) {
        _billArr = [NSMutableArray array];
    }
    return _billArr;
}

-(UITableView *)billTableView
{
    if (!_billTableView) {
        UITableView *tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KAScreenW, KAScreenH) style:UITableViewStylePlain];
        tv.tableFooterView = [UIView new];
        tv.tableHeaderView = [self billTableHeaderView];
        tv.delegate = self;
        tv.dataSource = self;
        tv.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tv registerNib:[UINib nibWithNibName:NSStringFromClass([KABillTableViewCell class]) bundle:nil] forCellReuseIdentifier:billTableViewCell];
        _billTableView = tv;
    }
    return _billTableView;
}

-(UIView *) billTableHeaderView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 70)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.view.bounds.size.width/2 - 25, 50)];
    leftLabel.text = [NSString stringWithFormat:@"今日收入 (+%ld)",_incomeSum];
    leftLabel.textColor = [UIColor blueColor];
    leftLabel.textAlignment = NSTextAlignmentCenter;
    
    KAButton *button = [[KAButton alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2 - 25, 3, 50, 50)];
    [button setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [button setTitle:@"记一笔" forState:UIControlStateNormal];
    [button setTitleColor:KANavTabColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button addTarget:self action:@selector(addBill:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2 + 25, 10, self.view.bounds.size.width/2 - 25, 50)];
    rightLabel.text = [NSString stringWithFormat:@"今日支出 (-%ld)",_outlaySum];
    rightLabel.textColor = [UIColor redColor];
    rightLabel.textAlignment = NSTextAlignmentCenter;
    
    [view addSubview:leftLabel];
    [view addSubview:rightLabel];
    
    self.incomeLabel = leftLabel;
    self.outlayLabel = rightLabel;
    return view;
}

-(void) loadIncomeWithOutlayData
{
    _incomeSum = 0;
    _outlaySum = 0;
    for (KADataBaseModel *dbModel in self.billArr) {
        if ([dbModel.billtype isEqualToString:@"支出"]) {
            _outlaySum += [dbModel.money integerValue];
        } else {
            _incomeSum += [dbModel.money integerValue];
        }
    }
    self.incomeLabel.text = [NSString stringWithFormat:@"今日收入 (+%ld)",_incomeSum];
    self.outlayLabel.text = [NSString stringWithFormat:@"今日支出 (-%ld)",_outlaySum];
    
    NSLog(@"income: + %ld,outlay: - %ld",_incomeSum,_outlaySum);
}

-(void) addBill:(UIButton *)sender
{
    KAAddBillViewController *addBill = [KAAddBillViewController new];
    addBill.currentDate = self.dateBtn.titleLabel.text;
    [self.navigationController pushViewController:addBill animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self setNavBar];
    
    
    
    [self.view addSubview:self.billTableView];
    
    UILabel *placeseText = [[UILabel alloc]init];
    placeseText.bounds = CGRectMake(0, 0, KAScreenW, KAScreenH);
    placeseText.center = CGPointMake(KAScreenW / 2, KAScreenH / 2);
    placeseText.text = @"今日暂无记录，赶快记一笔吧!";
    placeseText.textAlignment = NSTextAlignmentCenter;
    placeseText.textColor = KANavTabColor;
    placeseText.hidden = YES;
    [self.view addSubview:placeseText];
    _placeselabel = placeseText;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.billArr count] == 0) {
        _placeselabel.hidden = NO;
    } else {
        _placeselabel.hidden = YES;
    }
    return self.billArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KABillTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:billTableViewCell];
    if (!cell) {
        cell = [[KABillTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:billTableViewCell];
    }
    cell.dbMobel = self.billArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 94;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //KAAddBillViewController *addBill = [[KAAddBillViewController alloc]init];
    //addBill.dbModel = self.billArr[indexPath.row];
    //[self.navigationController pushViewController:addBill animated:YES];
}

@end
